package com.cn.ccp.action.course.online;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.SelectedCourse;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;

/**
 * 在线学习action
 * @author renyongcheng
 *
 */
public class OnlineAction extends BaseAction {
	private Logger log = Logger.getLogger(OnlineAction.class);
	private int totalNumber;//总共当前在线学习课程
	private String strCourseList;//当前学习课程列表
	private String strStudentId;
	
	public OnlineAction(){
		log.info("---------OnlineAction()--------");
		UserBean userBean = CCPContants.getUserBean();
		strStudentId = String.valueOf(userBean.getId());
		log.info("strStudentId = "+strStudentId);
		getpagedetail();
	}

	
	@Override
	public void getpagedetail() {
		if(strStudentId != null)
			totalNumber = getSelectCourseService().onlineCount(strStudentId);
		setItemCount(totalNumber);
		int currentpage = getCurrentPage();
		int pagesize =8;
		this.setPageSize(pagesize);
		int start = (currentpage - 1) * pagesize;
		if(strStudentId != null){
			List<SelectedCourse>  courseList = getSelectCourseService().onlineList(strStudentId,start, pagesize);
			initData(courseList);
		}
	}
	
	/**
	 * 初始化学生在线学习课程表
	 * @param courseList
	 */
	private void initData(List<SelectedCourse> courseList  ){
		StringBuffer buf = new StringBuffer();
		int i = 0;
		buf.append("<ul class='thumbnails'>");
		for(SelectedCourse b : courseList){
//			CCPContants.getFilePath(b.getImage() , getFilePathService(),b.getFilePathId() , CCPContants.getUserBean().getId() ,false,null);
//			if(!b.getImage().equals("book-default.png")){
//				String fileName = b.getImage().split("/")[1];
//				b.setImage(CCPContants.getUserBean().getId()+"/"+fileName);
//			}
			if(b.getImage()  != null && !"".equalsIgnoreCase(b.getImage().trim())){
				File sourseFile = null;
				if(b.getFilePathId() !=  -1)
					sourseFile = new File(getFilePathService().findFilePathById(b.getFilePathId()).getPath()+b.getImage());
				else
					sourseFile = new File(CCPContants.SYS_PATH+"files"+File.separator + b.getImage());
				
				File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId() + File.separator);
				if(des.mkdir()){
					des.mkdirs();
				}
				des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId()  + File.separator +sourseFile.getName());
				CCPContants.copyFile(sourseFile.getPath(), des.getPath());
				b.setImage(CCPContants.getCurrUserId()+"/"+des.getName());
			}
			
			
			
			String courseStatus = "";
			switch (b.getStatus()) {
			case 1:
				courseStatus = getMessages("onlien_test_page_score_status_1");
				break;
			case 2:
				courseStatus = getMessages("onlien_test_page_score_status_2");
				break;	
			case 3:
				courseStatus = getMessages("onlien_test_page_score_status_3");
				break;
			default:
				break;
			}
			if(i % 4 == 0 && i != 0){
				buf.append("</ul>");
				buf.append("<ul class='thumbnails'>");
			}
			buf.append("<li class='span3'>");
			buf.append("<a href='onlineDetail.faces?selectCourseId="+b.getId()+"&courseId="+b.getCourseId()+"' class='img'><img src='../../tempfile/"+(b.getImage() == null ? "images/book-01.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' ></a>");
			buf.append("<div class='caption'>");
			buf.append("<p title=\""+b.getCourserName()+"\">");
			buf.append(b.getCourserName().length() > 9 ? b.getCourserName().substring(0, 9)+"...<br/>" :b.getCourserName()+"<br/>");
			buf.append(getMessages("online_page_select_progress")+courseStatus+"<br />");
			buf.append(getMessages("online_page_select_teacher")+b.getTeacherName()+"<br />");
			if(!b.getStarLevel().equalsIgnoreCase("0"))
				buf.append("<span class='text-error'>"+b.getStarLevel()+"</span>"+getMessages("online_page_select_star")+"<br />");
			else
				buf.append("<span class='text-error'></span>"+getMessages("online_page_select_star_level_no")+"<br />");
			
			//判断是否已经评价
			if(b.isHasMarked())    
				buf.append("<span class='text-info'>"+b.getMarks()+"</span>"+getMessages("online_page_select_reply")+"&nbsp;&nbsp;<a href='onlineMarkList.faces?courseid="+b.getCourseId()+"&selectedCourseId="+b.getId()+"'>("+getMessages("online_page_select_marked_yes")+")</a><br />");
			else
				buf.append("<span class='text-info'>"+b.getMarks()+"</span>"+getMessages("online_page_select_reply")+"&nbsp;&nbsp;<a href='onlineMarkList.faces?courseid="+b.getCourseId()+"&selectedCourseId="+b.getId()+"'>("+getMessages("online_page_select_marked_no")+")</a><br />");
			
			buf.append("</p>");
			buf.append("</div>");
			buf.append("<div class='progress caption'>");
			buf.append("<div class='bar' style='width: "+(b.getFinishRate()*100)+"%;'></div>");
			buf.append(" </div>");
			buf.append(" &nbsp;&nbsp;<a href='onlineDetail.faces?selectCourseId="+b.getId()+"&courseId="+b.getCourseId()+"' class='btn btn-primary'>&nbsp;"+getMessages("online_page_action_study")+"</a>&nbsp;&nbsp;");
			buf.append("<a href='../course/studentCourseIssue.faces?id="+b.getCourseId()+"' class='btn'>&nbsp;&nbsp;"+getMessages("online_page_select_action_quest")+"&nbsp;</a><br/>");
			buf.append("</li>");
			i++;
		}
		buf.append("</ul>");
		strCourseList = buf.toString();
		log.info(strCourseList);
	}
	
	
	public int getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(int totalNumber) {
		this.totalNumber = totalNumber;
	}

	public String getStrCourseList() {
		return strCourseList;
	}

	public void setStrCourseList(String strCourseList) {
		this.strCourseList = strCourseList;
	}
	
	
	
}
